package cn.zw.frr.core;


import cn.zw.frr.ringbuffer.BaseCommond;

public abstract class Pipe<T> {


    BaseCommond in;

    BaseCommond pipe;

    String methodName;

    Class param;

    int count;

    public Pipe() {
        this.count = 1;
    }

    public Pipe(int count) {
        this.count = count;
    }

    public Pipe setIn(T arg) {
        if(in==null){
            this.in = inFunc(arg);
        }
        pipe = in;
        pipe.setCount(count);
        if(pipe.CAN_USE==false){
            pipe.go();
            pipe.CAN_USE=true;
        }
        return this;
    }

    public Pipe setIn(String methodName,Class param,T arg) {
        if(in==null){
            this.in = inFunc(arg);
        }
        pipe = in;
        pipe.setCount(count);
        pipe.setMethodName(methodName);
        pipe.setParam(param);
        if(pipe.CAN_USE==false){
            pipe.go();
            pipe.CAN_USE=true;
        }
        return this;
    }

    public Pipe setIn(Integer num,String methodName,Class param,T arg) {
        if(in==null){
            this.in = inFunc(arg);
        }
        pipe = in;
        pipe.setCount(count);
        pipe.setMethodName(methodName);
        pipe.setParam(param);
        pipe.setiBaseServiceNum(num);
        if(pipe.CAN_USE==false){
            pipe.go();
            pipe.CAN_USE=true;
            System.out.println(pipe.CAN_USE);
        }
        return this;
    }

    public Pipe setMethod(String methodName){
        this.methodName = methodName;
        return this;
    }

    public Pipe setMethod(String methodName, Class param){
        this.methodName = methodName;
        this.param = param;
        return this;
    }

    public Pipe setPipe(int count) {
        this.count = count;
        return this;
    }

    public BaseCommond exec(){

//        pipe=this.in;
//        System.out.println(pipe);
//        pipe.setCount(count);
//        if(methodName!=null){
//            pipe.setMethodName(methodName);
//            if(param!=null){
//                pipe.setParam(param);
//            }
//        }
        pipeFunc();
        return pipe;
    }

    public BaseCommond exec(Integer i){
//        pipe=this.in;
//        System.out.println(pipe);
//        pipe.setCount(count);
//        if(methodName!=null){
//            pipe.setMethodName(methodName);
//            if(param!=null){
//                pipe.setParam(param);
//            }
//        }
//        pipe.setiBaseServiceNum(i);
        pipeFunc();
        return pipe;
    }

    public abstract BaseCommond inFunc(T arg);

    public BaseCommond pipeFunc() {
        pipe.exec();
        return pipe;
    };


}
